/**
 * Created by gastrodia on 14-9-24.
 */

/**
 * Created by gastrodia on 14-9-24.
 */
var logger = require('../log').logger;

var Cache = function(maxTime,maxLength){
    var cache = {};

    this.cache = cache;
    this.maxTime = maxTime;
    this.maxLength = maxLength;
    this.length = 0;
    var that = this;
    //根据maxTime和maxLength 每隔10s清理一次
    setInterval(function(){
        var now = new Date().getTime();
        for(var key in cache){
            var time = cache[key].time;
            var subTime = now - time;
            if(subTime > maxTime){
                delete cache[key];
                that.length  = that.length - 1;
            }
        }

        logger.info('cache size ' + that.length);
    },10*1000);
};

Cache.prototype.set = function(key,value){
    this.cache[key] = {
        time:new Date().getTime(),
        value:value
    }
    this.length = this.length + 1;
};

Cache.prototype.get = function(key){
    return this.cache[key] && this.cache[key].value;
}


module.exports = Cache;